package top.zhangjianyong.tools.utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import top.zhangjianyong.tools.entity.Vegetable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ： zhangjianyong
 * @date ： 2022/9/22 5:25 PM
 */
public class CanteenHtmlUtil {

    /**
     * 解析菜单
     *
     * @param body
     * @param date
     * @return
     */
    public static List<Vegetable> getMenu(String body, String date,String cc) {
        Document doc = Jsoup.parse(body);

        List<Vegetable> list = new ArrayList<>();
        Elements elements = doc.select("li.mui-media");
        List<Element> elementList = elements.stream().collect(Collectors.toList());
        for (Element element : elementList) {
            Vegetable vegetable = new Vegetable();
            String img = element.getElementsByTag("img").get(0).attr("src");
            vegetable.setImg(img);
            Elements span = element.getElementsByTag("span");

            String name = element.select(".ylyTitle").text();
            String desc = element.select(".mui-ellipsis.product").text();
            String price = element.select(".price").text();
            String order = element.select(".number").text().split("已售")[1].split("/")[0];
            String stock = element.select(".number").text().split("已售")[1].split("/")[1];
            vegetable.setName(name);
            vegetable.setDescription(desc);
            vegetable.setPrice(price);
            vegetable.setOrder(Integer.parseInt(order));
            vegetable.setStock(Integer.parseInt(stock));
//javascript:jlgwc3('008404','11012','套餐A','','2022-12-26','2');
            String[] ontouchstarts = element.select(".iconfont.icon-jia-yuankuang")
                    .attr("ontouchstart")
                    .replace("javascript:jlgwc3", "")
                    .replace(";", "")
                    .replace("(", "").replace(")", "")
                    .replace("'", "").split(",");

            vegetable.setNo(ontouchstarts[0]);
            vegetable.setBizid(ontouchstarts[1]);
            vegetable.setDatecs(ontouchstarts[4]);
            vegetable.setCc(cc);

            vegetable.setEatDate(date);
            list.add(0, vegetable);
        }
        return list;
    }

    /**
     * 解析购物车
     *
     * @param body
     * @return
     */
    public static List<Vegetable> getCar(String body) {
        Document doc = Jsoup.parse(body);
        List<Vegetable> list = new ArrayList<>();
        Elements elements = doc.select(".mui-table-view-cell1.mui-media");
        for (Element element : elements) {
            Vegetable vegetable = new Vegetable();
            String name = element.select(".ylyTitle").text();
            String desc = element.select(".mui-ellipsis.product").text();
            vegetable.setName(name);
            vegetable.setDescription(desc);
            String date = element.select(".reduce").attr("ontouchstart")
                    .replace("javascript:delCartNew2", "")
                    .replace("'", "").split(",")[1];
            vegetable.setDatecs(date);
            list.add(vegetable);
        }

        return list;
    }

    public static String getAmt(String body) {
        Document doc = Jsoup.parse(body);
        return doc.getElementById("totalPrice").text();
    }

    public static List<String> getMenuUri(String body) {
        Document doc = Jsoup.parse(body);
        Elements elements = doc.getElementsByClass("cell");

        List<String> list = new ArrayList<>();
        for (Element element : elements) {
            String href = element.getElementsByTag("a").get(0).attr("href");
            list.add(href);
        }
        return list;
    }
}
